package com.api.user.controller;

import com.api.common.EmailUtil;
import com.api.core.annotation.PowerEnable;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.CompanionGame;
import com.api.user.model.UserFriend;
import com.api.user.model.UserInfo;
import com.api.user.service.UserFriendApplyService;
import com.api.user.service.UserFriendService;
import com.api.user.service.UserInfoService;
import io.swagger.annotations.*;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;



/**
* Created by wanghuiwen on 2021/03/31.
*/
@PowerEnable(name = "好友",url = "/user/friend")
@Api(value = "好友", tags = {"好友"})
@RestController
@RequestMapping("/user/friend")
public class UserFriendController extends Ctrl{

    @Resource
    private UserFriendService userFriendService;
    @Resource
    private UserFriendApplyService userFriendApplyService;

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "申请好友", tags = {"好友"}, notes = "申请好友")
    @PostMapping(value="/add",name="申请好友")
    public Result add(@RequestParam Long userId, Authentication authentication) {
        Result result = userFriendApplyService.add(userId,getAuthUser(authentication));
        if(result.getCode() == 200){
            UserInfo info = userInfoService.findById(userId);
            EmailUtil.gmailSender(info.getEmail(),"You have a friend request pending, let's see who is the mysterious person ! ",null);
        }
        return result;
    }

    @ApiOperation(value = "同意或者拒绝添加", tags = {"好友"}, notes = "同意或者拒绝添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "粉丝id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "同意/拒绝", dataType = "String", paramType = "query")
    })
    @PostMapping(value="/agree",name="申请好友")
    public Result agree(@RequestParam Long userId,@RequestParam Integer state, Authentication authentication) {
        Long idolId = getAuthUser(authentication).getId();

        return userFriendService.agree(idolId,userId,state);
    }

    @ApiOperation(value = "好友删除", tags = {"好友"}, notes = "好友删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId",required=true, value = "好友id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/remove",name="好友删除")
    public Result remove(@RequestParam Long userId, Authentication authentication) {
        Long idolId = getAuthUser(authentication).getId();
        return userFriendService.remove(idolId,userId);
    }


    @ApiOperation(value = "用户好友", tags = {"好友"}, notes = "用户好友")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "用户好友")
    public Result list(@RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       Authentication authentication) {
        Long idolId = getAuthUser(authentication).getId();
        return userInfoService.friendList(idolId, order, page, size);
    }


    @PostMapping(value = "apply/list", name = "用户好友申请")
    public Result applyList(@RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        return userFriendService.applyList(order,page,size,userId);
    }


    @PostMapping(value = "user/search", name = "用户搜索")
    public Result search(@RequestParam String uid,Authentication authentication) {

        return userFriendService.search(uid,getAuthUser(authentication));
    }
}
